#include <Arduino.h>

// 编写esp32s3舵机的驱动程序，采用ESP32Servo库，端口号是3
#include <ESP32Servo.h>

Servo servo1; // 舵机连接到GPIO3
// 平滑移动舵机的辅助函数
void smoothMoveServo(int start, int end, int returnTo) {
  const float pi = 3.1415926;
  int step = (start < end) ? 1 : -1;
  
  // 从start移动到end
  for (int i = start; i != end; i += step) {
    servo1.write(i);
    int delayTime = 40 * (sin((i * pi / 360.0) - pi / 2) + 1);
    delay(delayTime);
  }

  // 如果returnTo不等于-1，则返回到returnTo
  if (returnTo != -1) {
    step = (end < returnTo) ? 1 : -1;
    for (int i = end; i != returnTo; i += step) {
      servo1.write(i);
      int delayTime = 40 * (sin((i * pi / 360.0) - pi / 2) + 1);
      delay(delayTime);
    }
  }
}
void setup()
{
  Serial.begin(115200);
  // ESP32PWM::allocateTimer(0);
  // ESP32PWM::allocateTimer(1);
  // ESP32PWM::allocateTimer(2);
  // ESP32PWM::allocateTimer(3);
  servo1.setPeriodHertz(50);    // standard 50 hz servo
  servo1.attach(3, 1000, 2000); // Attach the servo after it has been detatched
}

void loop()
{
  smoothMoveServo(0, 180, 0);   // 从0度转到180度

}